package com.zhouz.admin.app.controller;

import com.zhouz.admin.app.annotation.RequiresPermissionsDesc;
import com.zhouz.admin.app.db.domain.SysSystem;
import com.zhouz.admin.app.db.service.LitemallSystemConfigService;
import com.zhouz.admin.app.db.vo.Result;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/config")
@Validated
@Log4j2
public class AdminConfigController {
    @Autowired
    private LitemallSystemConfigService systemConfigService;

    @RequiresPermissions("admin:config:list")
    @RequiresPermissionsDesc(menu = {"配置管理", "系统配置"}, button = "详情")
    @GetMapping("/list")
    public Result<Map<String, List<SysSystem>>> listMall() {
        return Result.success(systemConfigService.list());
    }
    @RequiresPermissions("admin:config:updateConfigs")
    @RequiresPermissionsDesc(menu = {"配置管理", "订单配置"}, button = "编辑")
    @PostMapping("/update")
    @Transactional(rollbackFor = RuntimeException.class)
    public Result updateOrder(@RequestBody Map<String, List<SysSystem>> body) {
        Map<String,String> data = new HashMap<>();
        body.forEach((k,v)->{
            v.forEach(x ->{
                data.put(x.getKeyName(),x.getKeyValue());
            });
        });
        systemConfigService.updateConfig(data);
        systemConfigService.config();
        return Result.success();
    }
}
